using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Security.AccessControl;





namespace ArchSoft
{
    public partial class Report : Form
    {
        DataTable reportDT = new DataTable("Report");
        DataRow row;
        DataColumn col;


        string ProjectPath;
        string[] kq;

        int colIndex = -1;
        int rowIndex = -1;

        string username = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();

        public Report()
        {
            InitializeComponent();

            col = new DataColumn();
            col.DataType = System.Type.GetType("System.String");
            col.ColumnName = "Name";
            col.Caption = "Name";
            reportDT.Columns.Add(col);

            col = new DataColumn();
            col.DataType = System.Type.GetType("System.String");
            col.ColumnName = "Date_Modify";
            col.Caption = "Date Modified";
            reportDT.Columns.Add(col);

            col = new DataColumn();
            col.DataType = System.Type.GetType("System.String");
            col.ColumnName = "Date_Create";
            col.Caption = "Date Created";
            reportDT.Columns.Add(col);

            col = new DataColumn();
            col.DataType = System.Type.GetType("System.String");
            col.ColumnName = "Date_Access";
            col.Caption = "Date Accessed";
            reportDT.Columns.Add(col);

            col = new DataColumn();
            col.DataType = System.Type.GetType("System.String");
            col.ColumnName = "Folder";
            col.Caption = "Folder";
            reportDT.Columns.Add(col);

            //col = new DataColumn();
            //col.DataType = System.Type.GetType("System.String");
            //col.ColumnName = "Size";
            //col.Caption = "Size";
            //reportDT.Columns.Add(col);





            dataGridView1.DataSource = reportDT;

            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;

            this.mnuViewDWG.Click += new EventHandler(mnuViewDWG_Click);
            this.mnuAutoCad.Click += new EventHandler(mnuAutoCad_Click);
        }

        void mnuAutoCad_Click(object sender, EventArgs e)
        {
            try
            {
                string path = dataGridView1[4, rowIndex].Value.ToString() + Path.DirectorySeparatorChar + dataGridView1[0, rowIndex].Value.ToString();
                System.Diagnostics.Process.Start(path);
            }
            catch (Exception ex)
            {

            }
        }

        void mnuViewDWG_Click(object sender, EventArgs e)
        {
            try
            {
                string path = dataGridView1[4, rowIndex].Value.ToString() + Path.DirectorySeparatorChar + dataGridView1[0, rowIndex].Value.ToString();
                //ViewDwgFrm view = new ViewDwgFrm(path);
                //view.ShowDialog();
                //ViewerMainForm frm = new ViewerMainForm();
                //frm.FileName = path;
                //frm.ShowDialog();
            }
            catch (Exception ex)
            {

            }
        }

        public void setProjectPath(string path)
        {
           ProjectPath = path;
        }

        public string getProjectPath()
        {
            return ProjectPath; 
        }

        private void comboBoxFolder_SelectedIndexChanged(object sender, EventArgs e)
        {
            //MessageBox.Show(comboBoxFolder.SelectedItem.ToString());

            try
            {
                string reportPath = Path.GetDirectoryName(ProjectPath) + Path.DirectorySeparatorChar + comboBoxFolder.SelectedItem.ToString();

                //MessageBox.Show(reportPath);

                kq = Directory.GetFiles(reportPath, "*.*", SearchOption.AllDirectories);

                // MessageBox.Show(kq.Length.ToString());

                if (reportDT.Rows.Count > 0)
                {
                    reportDT.Rows.Clear();
                }

                for (int i = 0; i < kq.Length; i++)
                {
                    row = reportDT.NewRow();
                    row["Name"] = Path.GetFileName(kq[i]);
                    row["Date_Modify"] = File.GetLastWriteTime(kq[i]);
                    row["Date_Create"] = File.GetCreationTime(kq[i]);
                    row["Date_Access"] = File.GetLastAccessTime(kq[i]);
                    row["Folder"] = Path.GetDirectoryName(kq[i]);

                    reportDT.Rows.Add(row);
                }

                dataGridView1.DataSource = reportDT;
                dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;


                toolStripStatusLabel3.Text = kq.Length.ToString() + " file(s) is found";

            }
            catch (Exception eee)
            {
                //MessageBox.Show(eee.Message);
            }

        }

        private void buttonClear_Click(object sender, EventArgs e)
        {
            reportDT.Rows.Clear();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
           //MessageBox.Show(dataGridView1["Folder", e.RowIndex].Value.ToString() + Path.DirectorySeparatorChar + dataGridView1["Name", e.RowIndex].Value.ToString());
        }

        private void dataGridView1_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                System.Diagnostics.Process.Start(dataGridView1["Folder", e.RowIndex].Value.ToString() + Path.DirectorySeparatorChar + dataGridView1["Name", e.RowIndex].Value.ToString());
            }
            catch(Exception eee)
            {
                //MessageBox.Show(eee.Message);
            }
        }

        private void Report_Load(object sender, EventArgs e)
        {
            toolStripStatusLabel1.Text = "Current project : " + Path.GetDirectoryName(ProjectPath).ToString();
            comboBoxFolder.SelectedIndex = 0;
        }

     //   FrmMain fr = new FrmMain();

        private void Report_FormClosed(object sender, FormClosedEventArgs e)
        {
           // fr.setFilePath("");
          //  MessageBox.Show(Directory.GetParent(ProjectPath).ToString());
           // AddAccessRuleAll();

        }
        public void AddAccessRule(string path)
        {
            DirectoryInfo dInfor = new DirectoryInfo(path);
            DirectorySecurity dSecurity = dInfor.GetAccessControl();
            dSecurity.AddAccessRule(new FileSystemAccessRule(username, FileSystemRights.FullControl, AccessControlType.Deny));
            dInfor.SetAccessControl(dSecurity);
        }

        public void AddAccessRuleAll()
        {
            try
            {

                //foreach (string dir in Directory.GetDirectories(getFilePath()))
                //{
                //    MessageBox.Show(dir);
                //}

                // MessageBox.Show(getFilePath());

                string[] dir = Directory.GetDirectories(Directory.GetParent(ProjectPath).ToString());
                for (int i = 0; i < dir.Length; i++)
                {
                    // MessageBox.Show(dir[i]);
                    AddAccessRule(dir[i]);
                }
                //string temp = Directory.GetParent(getFilePath()).ToString();
                //AddAccessRule(temp);
            }
            catch (Exception ex)
            {

            }
        }
        public void RemoveAccessRule(string path)
        {
            DirectoryInfo dInfor = new DirectoryInfo(path);
            DirectorySecurity dSecurity = dInfor.GetAccessControl();

            dSecurity.RemoveAccessRule(new FileSystemAccessRule(username, FileSystemRights.FullControl, AccessControlType.Deny));
            dInfor.SetAccessControl(dSecurity);
        }

        public void RemoveAccessRuleAll()
        {
            try
            {
                string[] dir = Directory.GetDirectories(Directory.GetParent(ProjectPath).ToString());
                for (int i = 0; i < dir.Length; i++)
                {
                    //MessageBox.Show(dir[i]);
                    RemoveAccessRule(dir[i]);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                colIndex = e.ColumnIndex;
                rowIndex = e.RowIndex;

                string ex = Path.GetExtension(dataGridView1[0,e.RowIndex].Value.ToString());
                //MessageBox.Show(ex);

                if (ex.IndexOf(".dwg") < 0)
                {
                    mnuOpenView.Visible = false;
                }
                else
                {
                    mnuOpenView.Visible = true;
                }
            }
        }

        private void contextMenuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            if(e.ClickedItem.Name == "mnuOpen")
            {
                string path = dataGridView1[4,rowIndex].Value.ToString();
                System.Diagnostics.Process.Start(path);
            }
        }

        private void buttonReport_Click(object sender, EventArgs e)
        {
            ReportFolder frm = new ReportFolder();
            int index = comboBoxFolder.SelectedIndex;
            frm.Folder = comboBoxFolder.Items[index].ToString() ;
            frm.Project = this.ProjectPath;
            frm.LoadDev();
            frm.LoadInf();
            frm.WindowState = FormWindowState.Maximized;
            frm.ShowDialog();
        }
    }
}